package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonFragment;
import com.fgs.common.adapter.FlowLayoutAdapter;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.FlexBoxLayoutManager;
import com.fgs.common.widget.TabSwitchView;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.contract.EvaluationContract;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.model.enty.evaluation.EvaluationDetailInfo;
import com.tianhui.consignortwofei.mvp.model.enty.evaluation.EvaluationInfo;
import com.tianhui.consignortwofei.mvp.presenter.EvaluationPresenter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.widget.edittext.MultiLineEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/5/3
 * @description 评价
 **/
public class EvaluationActivity extends UploadPictureActivity<EvaluationContract.View, EvaluationContract.Presenter> implements EvaluationContract.View {

    private static final int TYPE_POSITIVE = 1;
    private static final int TYPE_MEDIUM = 2;
    private static final int TYPE_NEGATIVE = 3;

    @BindView(R.id.activity_evaluation_evaluationTypeRadioGroup)
    RadioGroup mRadioGroup;
    @BindView(R.id.activity_evaluation_positiveEvaluationRadioGroup)
    RadioButton mPositiveEvaluationRadioButton;
    @BindView(R.id.activity_evaluation_evaluationContentEditText)
    MultiLineEditText mContentEditText;
    @BindView(R.id.activity_evaluation_recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.activity_evaluation_blackListTabSwitchView)
    TabSwitchView mBlackListTabSwitchView;
    @BindView(R.id.activity_evaluation_typeRecyclerView)
    RecyclerView mTypeRecyclerView;


    @BindView(R.id.rb_yunshu)
    RatingBar rb_yunshu;
    @BindView(R.id.rb_anquan)
    RatingBar rb_anquan;
    @BindView(R.id.rb_fuwu)
    RatingBar rb_fuwu;
    @BindView(R.id.rb_zhuangche)
    RatingBar rb_zhuangche;
    @BindView(R.id.rb_xieche)
    RatingBar rb_xieche;




    private FlowLayoutAdapter mTypeAdapter;
    private EvaluationInfo mEvaluationInfo;
    private List<DictionaryInfo> mPositiveTypeList;
    private List<DictionaryInfo> mNegativeTypeList;
    private List<DictionaryInfo> mMediumTypeList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_evaluation;
    }

    @Override
    public EvaluationContract.Presenter createPresenter() {
        return new EvaluationPresenter(this);
    }

    @Override
    public EvaluationContract.View createView() {
        return this;
    }


    @Override
    public void init() {
        super.init();
        setTitle("发表评价");
        mEvaluationInfo = new EvaluationInfo();
        mPositiveEvaluationRadioButton.setChecked(true);
        mEvaluationInfo.isstate = TYPE_POSITIVE;
        mEvaluationInfo.isblack = "0";

        Bundle bundle = getIntent().getExtras();
        if (null != bundle && !bundle.isEmpty()) {
            OrderInfo info = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            mEvaluationInfo.dispatchno = info.dispatchno;
        }

        mTypeAdapter = new FlowLayoutAdapter<EvaluationDetailInfo>();
        mTypeAdapter.setMaxSelectNum(3);
        mTypeAdapter.setIsMultiSelectMode(true);
        mTypeRecyclerView.setLayoutManager(new FlexBoxLayoutManager(this));
        mTypeRecyclerView.setAdapter(mTypeAdapter);
        requestEvaluationType(TYPE_POSITIVE);

        initListener();
    }


    @Override
    public RecyclerView getPictureRecyclerView() {
        return mRecyclerView;
    }

    @Override
    public String getPictureModule() {
        return "orderEvaluate";
    }


    private void initListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.activity_evaluation_positiveEvaluationRadioGroup:
                        mEvaluationInfo.isstate = TYPE_POSITIVE;
                        if (mPositiveTypeList != null) {
                            mTypeAdapter.clear();
                            mTypeAdapter.setRealList(mPositiveTypeList);
                        } else {
                            requestEvaluationType(TYPE_POSITIVE);
                        }
                        break;
                    case R.id.activity_evaluation_mediumEvaluationRadioGroup:
                        if (mMediumTypeList != null) {
                            mTypeAdapter.clear();
                            mTypeAdapter.setRealList(mMediumTypeList);
                        } else {
                            requestEvaluationType(TYPE_MEDIUM);
                        }

                        mEvaluationInfo.isstate = TYPE_MEDIUM;
                        break;
                    case R.id.activity_evaluation_negativeEvaluationRadioGroup:
                        if (mNegativeTypeList != null) {
                            mTypeAdapter.clear();
                            mTypeAdapter.setRealList(mNegativeTypeList);
                        } else {
                            requestEvaluationType(TYPE_NEGATIVE);
                        }
                        mEvaluationInfo.isstate = TYPE_NEGATIVE;
                        break;
                }
                mTypeAdapter.multiSelectReset();
            }
        });

        mTypeAdapter.setOnItemClickListener(new RecyclerViewHolder.OnItemClickListener<DictionaryInfo>() {
            @Override
            public void onItemClick(View itemView, DictionaryInfo item, int position) {
                mTypeAdapter.select(position);

            }
        });

        mBlackListTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                mEvaluationInfo.isblack = value;
            }
        });





        rb_yunshu.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });

        rb_anquan.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });

        rb_fuwu.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });

        rb_zhuangche.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });

        rb_xieche.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });



    }

    private void requestEvaluationType(int type) {
        Map<String, String> map = new HashMap<>();
        map.put("pid", type + "");
        getPresenter().requestEvaluationType(map, type, true);

    }

    @OnClick(R.id.activity_evaluation_commitButton)
    public void commit() {
        String evaluationContent = mContentEditText.getContentText();

        if (TextUtils.isEmpty(evaluationContent)) {
            showToast("请填写评价内容");
            return;
        }
        mEvaluationInfo.evaluatecontent = evaluationContent;
        String selectPictureId = getSelectPictureId();
        mEvaluationInfo.evaluateimg = selectPictureId;
        mEvaluationInfo.type = 2;
        mEvaluationInfo.driverid = SharedPreferenceManager.getInstance().getConsignorId();


        if (TextUtils.isEmpty(mEvaluationInfo.dispatchno)) {
            LogUtil.print("获取订单信息失败，未获取到调度单号dispathno");
            return;
        }
        double integral = 0;
        List<DictionaryInfo> list = mTypeAdapter.getMultiContentInfo();
        if (list != null && list.size() > 0) {
            int size = list.size();
            EvaluationDetailInfo detailInfo;
            mEvaluationInfo.orderEvaluateDetails = new ArrayList<>();

            for (int i = 0; i < size; i++) {
                detailInfo = new EvaluationDetailInfo();
                detailInfo.type = list.get(i).num;
                detailInfo.integral = list.get(i).tips;
                integral += Double.parseDouble(list.get(i).tips);
                mEvaluationInfo.orderEvaluateDetails.add(detailInfo);
            }
        }
        mEvaluationInfo.integral = integral + "";
        LogUtil.print(mEvaluationInfo.toString());
        getPresenter().requestEvaluationCommit(mEvaluationInfo, true);

    }


    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void activityFinish() {
        finish();
    }

    @Override
    public void showEvaluationType(int type, List<DictionaryInfo> list) {

        switch (type) {
            case TYPE_POSITIVE:
                mPositiveTypeList = list;
                break;
            case TYPE_MEDIUM:
                mMediumTypeList = list;
                break;
            case TYPE_NEGATIVE:
                mNegativeTypeList = list;
                break;
        }
        mTypeAdapter.clear();
        mTypeAdapter.getData().addAll(list);
        mTypeAdapter.notifyDataSetChanged();

    }

    @Override
    public void showEvaluationSuccessDialog() {
        new OperateTipDialog.Builder(this)
                .title("提示")
                .content("评价提交成功")
                .positiveText("返回")
                .isCancelable(false)
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        activityFinish();
                        EventBusManage.operateActivityFinish();
                        EventBusManage.refreshOrderDetail();
                    }
                })
                .show();
    }

    @Override
    protected boolean isShowFinishTipDialog() {
        return true;
    }
}
